package com.hnust.root.web.controller.forward.user;

import cn.hutool.json.JSONObject;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.hnust.root.common.base.response.BaseRestController;
import com.hnust.root.common.base.response.BaseResult;
import com.hnust.root.dao.entity.domain.forward.SystemMessage;
import com.hnust.root.dao.entity.domain.forward.UserMessage;
import com.hnust.root.service.forword.wx.MessageService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/group/user")
public class WxMessageController extends BaseRestController {
    @Autowired
    private MessageService messageService;

    /**
     * 获取用户未读的消息数量
     */
    @ApiOperation("获取用户未读消息的数量")
    @PostMapping("/getUserUnReadMessageCount")
    public BaseResult<Integer> getUserUnReadMessageCount() {
        String openId = getWxAccountOpenId();
        return fillSuccessData(messageService.getUserUnReadMsgCount(openId));
    }


    @ApiOperation("获取用户消息")
    @PostMapping("/getUserMessage")
    public BaseResult<List<UserMessage>> getUserMessage() {
        String openId = getWxAccountOpenId();
        List<UserMessage> userMessages = messageService.getUserMassage(openId);
        return fillSuccessData(userMessages);
    }

    @ApiOperation("用户阅读消息")
    @PostMapping("/readMessage")
    public BaseResult readMessage(@RequestBody JSONObject jsonObject) {
        int msgId = jsonObject.getInt("mid");
        boolean res = messageService.readUserMessage(msgId);
        return res ? fillSuccessData() : fillFailData();
    }

    //获取系统消息
    @ApiOperation("获取所有的系统消息")
    @PostMapping("/getSystemMessage")
    public BaseResult<List<SystemMessage>> getSystemMessage() {
        String openId = getWxAccountOpenId();
        return fillSuccessData(messageService.getAllSysMsg(openId));
    }

    @ApiOperation("阅读系统消息")
    @PostMapping("systemMessageRead")
    public BaseResult systemMessageRead(@RequestBody JSONObject jsonObject) {
        int msgId = jsonObject.getInt("id");
        messageService.readSysMsg(msgId);
        return fillSuccessData();
    }
}
